package org.jash.report.model

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.jash.common.mvi.BaseState
import org.jash.common.mvi.BaseViewModel
import org.jash.common.net.retrofit
import org.jash.report.net.ReportService

class ReportViewModel:BaseViewModel<ReportIntent, ReportState>() {
    val service by lazy { retrofit.create(ReportService::class.java) }
    fun selReport(sel:ReportIntent.SelReport) {
        viewModelScope.launch {
            state.value = try {
                val res = service.selReport(mapOf("userId" to sel.userId, "page" to sel.page))
                if (res.statusCode == 200) {
                    ReportState.ReportList(res.data)
                } else {
                    BaseState.Error(res.msg)
                }
            } catch (e:Exception) {
                BaseState.Error(e.message ?: "")
            }
        }
    }
}